//
//  CLButtonExtension.m
//  toroBracelet
//
//  Created by 丁益 on 16/7/28.
//  Copyright © 2016年 celi. All rights reserved.
//

#import "CLButtonExtension.h"

@interface CLButtonExtension()

@property (nonatomic ,weak) UILabel *titelLabel;
@property (nonatomic ,weak) UIImageView *imageView;
@property (nonatomic, copy) CLButtonOnclickBlock block;

@end
@implementation CLButtonExtension

- (id)initWithSize:(CGSize)size Image:(NSString *)imageStr titel:(NSString *)titel onclickBlock:(CLButtonOnclickBlock)onclickBlock{

    
    CGRect rect = CGRectMake(0, 0, size.width,size.height);
    self = [super initWithFrame:rect];
    
    if (self) {
        self.block = onclickBlock;
        UIButton *viewBg = [[UIButton alloc]initWithFrame:rect];
        [viewBg addTarget:self action:@selector(viewBgOnclick:) forControlEvents:UIControlEventTouchUpInside];
        viewBg.backgroundColor = [UIColor clearColor];
        
        CGFloat imageViewW = 0;
        CGFloat imageViewH = 0;
        if (titel == nil) {
            imageViewW = rect.size.width;
            imageViewH = imageViewW;
        } else {
             imageViewW = rect.size.width*0.5;
             imageViewH = imageViewW;
        }
     
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageViewW,imageViewH)];
        imageView.center = CGPointMake(viewBg.width/2, imageView.height/2);
        imageView.image = [UIImage imageNamed:imageStr];
        self.imageView = imageView;
        [viewBg addSubview:imageView];
        
        if (titel != nil) {
            CGFloat titelLabelY = CGRectGetMaxY(imageView.frame);
            UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, titelLabelY, viewBg.width, (viewBg.height-titelLabelY))];
            titelLabel.text = titel;
            titelLabel.textColor = DYColor(240, 240, 240);
            titelLabel.textAlignment = NSTextAlignmentCenter;
            self.titelLabel = titelLabel;
            [viewBg addSubview:titelLabel];
        }
        [self addSubview:viewBg];
    }
    
    return self;
}

- (id)initWithConstomImageSize:(CGSize)imageSize titelSize:(CGSize)titelSize Image:(NSString *)imageStr titel:(NSString *)titel onclickBlock:(CLButtonOnclickBlock)onclickBlock{
    
    CGFloat width = 0;
    if (imageSize.width > titelSize.width) {
        width = imageSize.width;
    } else {
        width = titelSize.width;
    }
    CGFloat height = imageSize.height + titelSize.height;
    
    CGRect rect = CGRectMake(0, 0, width,height);
    self = [super initWithFrame:rect];
    
    if (self) {
        self.block = onclickBlock;
        UIButton *viewBg = [[UIButton alloc]initWithFrame:rect];
        [viewBg addTarget:self action:@selector(viewBgOnclick:) forControlEvents:UIControlEventTouchUpInside];
        viewBg.backgroundColor = [UIColor clearColor];
    
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageSize.width,imageSize.height)];
        imageView.center = CGPointMake(viewBg.width/2, imageView.height/2);
        imageView.image = [UIImage imageNamed:imageStr];
        self.imageView = imageView;
        [viewBg addSubview:imageView];
        
        if (titel != nil) {
            CGFloat titelLabelY = CGRectGetMaxY(imageView.frame);
            UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, titelLabelY, titelSize.width, titelSize.height)];
            titelLabel.text = titel;
            titelLabel.textColor = DYColor(240, 240, 240);
            titelLabel.textAlignment = NSTextAlignmentCenter;
            self.titelLabel = titelLabel;
            [viewBg addSubview:titelLabel];
        }
        [self addSubview:viewBg];
    }
    
    return self;
}

+ (id)buttonExtensionWithSize:(CGSize)size Image:(NSString *)imageStr titel:(NSString *)titel onclickBlock:(CLButtonOnclickBlock)onclickBlock {

    return [[self alloc] initWithSize:size Image:imageStr titel:titel onclickBlock:onclickBlock];
}

+ (id)buttonExtensionWithConstomImageSize:(CGSize)imageSize titelSize:(CGSize)titelSize Image:(NSString *)imageStr titel:(NSString *)titel onclickBlock:(CLButtonOnclickBlock)onclickBlock{
    return [[self alloc] initWithConstomImageSize:imageSize titelSize:titelSize Image:imageStr titel:titel onclickBlock:onclickBlock];
}



- (void)viewBgOnclick:(UIButton*)sender{

    if (self.block) {
        self.block();
    }
}

- (void) refreshWithImageStr:(NSString*)imageStr titel:(NSString*)titel{

    if (imageStr) {
        self.imageView.image = [UIImage imageNamed:imageStr];
    }
    if (titel) {
        self.titelLabel.text = titel;
    }
}

- (void)setTitelColor:(UIColor*)color{

    self.titelLabel.textColor = color;
}
- (void)setTitelFont:(UIFont*)font{
    self.titelLabel.font = font;
}



@end
